package com.yuan.model_community.service.impl;

import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_community.domain.CommunityFacilities;
import com.yuan.model_community.repository.CommunityFacilitiesDao;
import com.yuan.model_community.service.CommunityFacilitiesService;
import com.yuan.model_community.service.dto.CommunityFacilitiesDto;
import com.yuan.model_community.service.mapstruct.CommunityFacilitiesMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Service
@RequiredArgsConstructor
public class CommunityFacilitiesServiceImpl implements CommunityFacilitiesService {

    private final CommunityFacilitiesDao communityFacilitiesDao;

    private final CommunityFacilitiesMapper communityFacilitiesMapper;

    @Override
    public PageResult<CommunityFacilitiesDto> queryAll(CommonQueryCriteria criteria, Pageable pageable) {
        Page<CommunityFacilities> page = communityFacilitiesDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(communityFacilitiesMapper::toDto));
    }

    @Override
    public List<CommunityFacilitiesDto> queryAll(CommonQueryCriteria criteria) {
        List<CommunityFacilities> users = communityFacilitiesDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        return communityFacilitiesMapper.toDto(users);
    }

    @Override
    public void download(List<CommunityFacilitiesDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CommunityFacilitiesDto communityNoticeDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("设施Id", communityNoticeDto.getFaId());
            map.put("设施名称", communityNoticeDto.getFaName());
            map.put("负责人", communityNoticeDto.getFaPerson());
            map.put("负责人名", communityNoticeDto.getFaName());
            map.put("设施描述", communityNoticeDto.getFaText());
            map.put("联系电话", communityNoticeDto.getPhoneNumber());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(CommunityFacilities resources) {
//        if (communityFacilitiesDao.findCommunityNoticeByUserId(resources.getUserId()) != null) {
//            throw new EntityExistException(CommunityNotice.class, "userId", String.valueOf(resources.getUserId()));
//        }
//        if (communityFacilitiesDao.findCommunityNoticeByFullName(resources.getFullName()) != null) {
//            throw new EntityExistException(CommunityNotice.class, "fullName", resources.getFullName());
//        }
        communityFacilitiesDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CommunityFacilities resources) throws Exception {
        CommunityFacilities communityNotice = communityFacilitiesDao.findById(resources.getFaId()).orElseGet(CommunityFacilities::new);
        ValidationUtil.isNull(communityNotice.getFaId(), "CommunityFacilities", "faId", resources.getFaId());
        communityFacilitiesDao.save(communityNotice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        communityFacilitiesDao.deleteById(ids);
    }
}
